<?php

declare(strict_types=1);

namespace App\Model\PssModel;

use App\Model\BaseModel\BasePssModel;
use Hyperf\DbConnection\Db;
class OrderSupplierGoods extends BasePssModel
{
    public $table = 'order_supplier_goods';
    public $goods_fields = [
        'id',
        'goods_name',
        'order_id',
        'supplier_skucode',
        'supplier_skuid',
        'supplier_goods_type',
        'supplier_store_id',
        'supplier_store_code',
        'supplier_store_parameters',
        'quantity',
        'price',
        'barcode',
        'skucode',
        'weight',
        'batch_no',
        'goods_id',
        'store_id',
        'stock_id',
        'is_deduct_stock',
        'spec_num',
        'spec_name',
        'tax_rate',
        'has_rate',
        'costprice',
        'settlement_price',
        'goods_type',
        'declare_skucode',
        'declare_goods_name',
        'declare_spec',
        'declare_desc',
        'declare_barcode',
        'declare_unit_code',
        'declare_currency_code',
        'declare_country_code',
        'supplier_store_code',
        'declare_hscode',
        'ebc_name',
        'ebc_code',
        'tax'
    ];
    

    //商品类型（0-一般贸易，1-保税,2-海外直邮,3海外购）
    public static $goodsTypeArr = [
        '1' => '保税',
        '2' => '海外直邮',
        '0' => '一般贸易',
        '3' => '海外购'
    ];
    
    
    public function getSupplierOrderGoodsByOrderIds( $orderIds = [] )
    {
        return self::query()
        ->select([
            Db::raw('concat(goods_id,"") as goods_id'),
            Db::raw('concat(order_id,"") as order_id'),
            'goods_name',
            'image',
            'settlement_price',
            'quantity',
            'batch_no',
            'spec_num',
            'spec_name',
            'price',
            'tax',
            'tax_rate',
            'goods_type',
            'freight',
            'barcode',
        ])
        ->whereIn('order_id',$orderIds)
        ->get()
        ->toArray();
    }

    public function getSupplierOrderGoodsByOrderId( $orderId )
    {
        return self::query()
        ->select([
            'goods_name as goodsName',
            'image',
            'quantity',
            'spec_name as specName',
            'spec_num as specNum',
            'quantity',
            'price',
            'goods_type as goodsType',
            'tax',
            'tax_rate as taxRate'
        ])
        ->where('order_id',$orderId)
        ->get()
        ->toArray();
    }
    
    /**
     * 根据订单ID和商品条码获取订单数据
     * @param string $orderId
     * @param array $skucodes
     */
    public function getOrderGoodsSkuItems(string $orderId,array $skucodes )
    {
        return self::query()
        ->select([
            'id',
            'goods_name',
            'quantity',
            'spec_num',
            'price',
            'goods_type',
            'lower_rate',
            'skucode',
            'supplier_skucode',
        ])
        ->where('order_id',$orderId)
        ->whereIn('skucode',$skucodes)
        ->get()
        ->toArray();
    }
}
